Chapter 2: Installation of Development Software

Chapter 2: Setting Up Ubuntu for Vala and GTK 3.0+ Development in Visual Studio Code


2.1 Introduction


Welcome to the exciting world of Vala and GTK 3.0+ development on Ubuntu! This chapter will guide you through setting up your Ubuntu system for developing applications with Vala and GTK 3.0+ using Visual Studio Code (VS Code), one of the most popular and versatile code editors available today. By the end of this chapter, you’ll have a fully configured development environment ready for your Vala and GTK 3.0+ projects.


2.2 Installing Dependencies


Before diving into the setup, you need to install a few essential tools and libraries. Open your terminal and execute the following commands:

sudo apt update && sudo apt upgrade

This command updates the list of available packages and their versions and installs the newest versions of the packages you have.

Install Vala and GTK 3.0+

sudo apt install valac libgtk-3-dev

This installs the Vala compiler (valac) and the GTK 3.0+ development libraries.

Install Additional Tools

sudo apt install git build-essential

This command installs Git, a version control system, and build-essential, which includes compilers and libraries necessary for building software.


Setting Up Visual Studio Code

Visual Studio Code (VS Code) is a free, powerful open-source editor that supports a wide range of programming languages and frameworks. Follow these steps to set it up for Vala and GTK 3.0+ development:

Install Visual Studio Code

Visit the VS Code website and download the .deb package for Ubuntu.

Install it using the command:

sudo dpkg -i code_*.deb

Launch VS Code from your applications menu.


Install Vala Extensions

In VS Code, go to the Extensions view by clicking on the square icon on the sidebar, or pressing Ctrl+Shift+X.

Search for “Vala” and install the extension named “Vala”. This extension provides Vala language support in VS Code.

Configure Build Tasks


To compile Vala programs directly from VS Code, you need to set up a build task.


Press Ctrl+Shift+P to open the command palette and type “Tasks: Configure Task”, then select it.


Optional: Select “Create tasks.json file from template” and then choose “Others”.


Replace the content of tasks.json with the following:

{

  "version": "2.0.0",

  "tasks": [

    {

      "label": "Build Vala App",

      "type": "shell",

      "command": "valac --pkg gtk+-3.0 ${file} -o ${fileBasenameNoExtension}",

      "group": {

        "kind": "build",

        "isDefault": true

      }

    }

  ]

}

This task will compile the open Vala file with GTK 3.0+ support and create an executable.


2.4 Testing Your Setup

To ensure everything is set up correctly, let's create a simple Vala application:

Create a New File

In VS Code, create a new file and save it as hello.vala.

Write a Simple GTK Application

In Visual Studio Code create a new file called ValaCourse.vala

Copy the following code into the file ValaCourse.vala:


using Gtk;


int main (string[] args) {

  Gtk.init (ref args);


  var window = new Window ();

  window.title = "Hello, Vala!";

  window.border_width = 10;

  window.window_position = WindowPosition.CENTER;

  window.set_default_size (350, 70);

  window.destroy.connect (Gtk.main_quit);


  var label = new Label ("Hello, GTK+ 3.0!");

  window.add (label);


  window.show_all ();

  Gtk.main ();

  return 0;

}

Compile String:

In Terminal type to compile:

valac -X -w -g  ValaCourse.vala --pkg gtk+-3.0

Run the App

In Terminal type:

./ValaCourse




Build and Run the Application


Press Ctrl+Shift+B to run the build task. This will compile the Vala code.


Run the generated executable in the terminal:

./hello.exe

./hello

A window should appear displaying "Hello, GTK+ 3.0!".


2.5 Conclusion


Congratulations! You have successfully set up your Ubuntu environment for developing Vala and GTK 3.0+ applications using Visual Studio Code. This setup provides a robust foundation for developing sophisticated and visually appealing applications for the GNOME desktop environment. As you progress through the next chapters, you will delve deeper into Vala programming and explore the rich features of GTK 3.0+.

This chapter provides a concise yet comprehensive guide for beginners to set up their Ubuntu system for Vala and GTK 3.0+ development, ensuring they are ready to start building applications right away.