Ballerina Installation and Project Setup Simplified for Beginners
Introduction
Ballerina is an open-source, cloud-native programming language that makes it easy to create and deploy services with a focus on data handling and integration. Whether you’re building APIs, handling data streams, or creating complex applications, Ballerina is optimized to make these tasks simpler. This guide will walk you through setting up Ballerina on your local machine so you can start building your first project right away.
Downloading Ballerina
Visit the Ballerina Download Page.
Select the appropriate installer for your operating system (Windows, macOS, or Linux).
Once the download is complete, you’re ready to start the installation process.
Installing Ballerina
Windows
Run the downloaded
.msi
installer.Follow the installation wizard steps, and make sure the Add Ballerina to PATH option is selected so you can access it from the command line.
After installation, open Command Prompt or PowerShell to verify the installation.
macOS
Open the downloaded
.pkg
file and go through the installer steps.Alternatively, if you use Homebrew, you can install Ballerina by running:
brew install ballerina
To verify the installation, open the Terminal.
Linux
Extract the downloaded
.zip
file.Move the extracted folder to
/usr/lib/
(or any preferred location).Add Ballerina to your PATH by adding the following line to your
.bashrc
or.zshrc
file:export PATH=$PATH:/usr/lib/ballerina/bin
Refresh your terminal session by running:
source ~/.bashrc
Open a terminal to verify the installation.
Verifying the Installation
To confirm Ballerina is installed correctly, open a terminal or command prompt and run:
bal -v
If Ballerina is installed successfully, this command will display the version number. You’re now ready to create a Ballerina project!
Setting Up a New Ballerina Project
Ballerina organizes projects with a specific structure, making it easy to manage code, dependencies, and modules.
Create a New Project Folder: Choose or create a directory where you want your project to reside.
Initialize the Project: Run the following command in your project folder:
bal new my_project
Replace
my_project
with your desired project name.
This command will set up the following directory structure:
my_project
├── Ballerina.toml # Project configuration file
└── main.bal # Main Ballerina file
Understanding Project Files
Ballerina.toml: This configuration file defines project metadata, dependencies, and settings.
main.bal: This is the main entry point for your Ballerina application.
Writing Your First Ballerina Program
Navigate to your project directory and open main.bal
in a text editor. Replace the contents with the following “Hello, World!” code:
import ballerina/io;
public function main() {
io:println("Hello, World!");
}
Running Your Ballerina Program
To run your program, use the following command in the terminal:
bal run
You should see the output:
Hello, World!
Installing Additional Ballerina Packages (Optional)
If your project needs additional libraries, Ballerina’s package management system makes it easy to add them.
Search for packages on the Ballerina Central repository.
To add a package, use:
bal add <package-name>
These packages will be automatically included in your project, and you can use them by importing them into your code.
Testing Your Ballerina Project
Ballerina includes a built-in testing framework. To create tests:
Add a test file in your project’s test directory.
Write test functions, then run tests with:
bal test
Building and Packaging Your Ballerina Project
When you’re ready to deploy your project, you can build it into a standalone .jar
file. Run:
bal build
The output .jar
file will be available in the target/bin
directory, making it easy to deploy and run anywhere.
Conclusion
You’ve now set up Ballerina on your machine and created a basic project. From here, you can explore Ballerina’s extensive capabilities for cloud-native and data-centric applications. As you get more familiar, dive into creating APIs, integrating with databases, or handling complex data structures with ease.
Subscribe to my newsletter
Read articles from Faria Karim directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Faria Karim
Faria Karim
Currently, I am working as an "Associate Software Engineer" at "Kaz Software". I have completed my graduation from North South University in the year 2020 with the highest distinction. I always love to explore new ideas. I am a curious and self-motivated person. So, I always try to keep learning and share my ideas with other people. I enjoy problem-solving a lot. I have solved around 100+ problems in various online judges and I am also a three-star coder at Hackerrank. Moreover, I have participated in the ACM ICPC two times. I have completed the off-sight round of ACM ICPC of 2018. I have some basic knowledge of c/c++, Java, Javascript, Php, and Python. I have done several projects using these programming languages. I have hands-on experience on various backend frameworks like Node.js, Django, Flask, and Laravel. I have also used React.js as the front-end library for a few of my projects. Currently, I am more into React.js. For web-based projects, I have worked on both MySQL and NoSQL (MongoDB) as the database system. Machine learning is my another matter of interest. I have research experience in the fuzzy system. I have done a few projects using Tensorflow, Keras, CNN, and LSTM. Recently I have grown my interest in game development. I have built two games. One is using construct 2 and the another one is in Unity 3D. Side by side, I am also trying to figure out the things on ionic. In my free time, I mostly like to surf the internet, watch movies, and also love to do animations and illustrations using Powerpoint.