# Ubuntu gmsh import

**URL:** <https://fenicsproject.discourse.group/t/ubuntu-gmsh-import/16637>\
**Category:** General\
**Created:** [December 27, 2024, 7:49am UTC](https://fenicsproject.discourse.group/t/ubuntu-gmsh-import/16637 "2024-12-27T07:49:41Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![zeynep](https://avatars.discourse-cdn.com/v4/letter/z/73ab20/32.png) [@zeynep](https://fenicsproject.discourse.group/u/zeynep)\
**Post date:** [December 27, 2024, 7:49am UTC](https://fenicsproject.discourse.group/t/ubuntu-gmsh-import/16637/1 "2024-12-27T07:49:41Z")

</div>

Hi!  
I am using Ubuntu 24.04 and encountering issues when importing the Gmsh module in Python. What steps should I follow to resolve this problem? Or would you recommend using Docker to avoid such version-related issues? I would greatly appreciate it if you could help.

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [December 27, 2024, 8:46am UTC](https://fenicsproject.discourse.group/t/ubuntu-gmsh-import/16637/2 "2024-12-27T08:46:03Z")

</div>

This is not a GMSH forum. Please contact the Gmsh developers at:

> **[Issues · gmsh / gmsh · GitLab](https://gitlab.onelab.info/gmsh/gmsh/-/issues)**
>
> The main Gmsh project (https://gmsh.info)

If the problem is related to `dolfinx.io.gmshio`, you need to provide more context, see next paragraph.

You have not provided sufficient amount of information about your problem.  
For a future post, either for the Gmsh developers or here, please consider adding the following to your post:

1. how did you install Gmsh
2. What error message are you getting
3. What code are you running to get this error message

---

<div class="post-metadata">

**Author:** ![zeynep](https://avatars.discourse-cdn.com/v4/letter/z/73ab20/32.png) [@zeynep](https://fenicsproject.discourse.group/u/zeynep)\
**Post date:** [December 29, 2024, 10:45am UTC](https://fenicsproject.discourse.group/t/ubuntu-gmsh-import/16637/3 "2024-12-29T10:45:55Z")

</div>

Hello again, here is more detailed information about the problem I’m experiencing. We are using WSL, so when we download GMSH, it gets installed on Windows, but our Ubuntu environment is in WSL.

I want to create a 2D hexagonal geometry and then mesh it with this code.

```auto
import gmsh
from dolfinx.io import gmshio
from mpi4py import MPI
import matplotlib.pyplot as plt
from dolfinx.plot import plot
import pyvista as pv
from dolfinx.mesh import create_mesh

gmsh.initialize()
gmsh.model.add("Hexagon")

points = [
    gmsh.model.occ.addPoint(0, 0, 0),
    gmsh.model.occ.addPoint(1, 0, 0),
    gmsh.model.occ.addPoint(1.5, 1, 0),
    gmsh.model.occ.addPoint(1, 2, 0),
    gmsh.model.occ.addPoint(0, 2, 0),
    gmsh.model.occ.addPoint(-0.5, 1, 0),
]

lines = [
    gmsh.model.occ.addLine(points[i], points[(i + 1) % len(points)])
    for i in range(len(points))
]

loop = gmsh.model.occ.addCurveLoop(lines)
surface = gmsh.model.occ.addPlaneSurface([loop])

gmsh.model.occ.synchronize()
gmsh.model.mesh.generate(2)

mesh, cell_tags, facet_tags = gmshio.model_to_mesh(
    gmsh.model, MPI.COMM_WORLD, 0, gdim=2
)

gmsh.finalize()

plotter = pv.Plotter()
plotter.add_mesh(pv.wrap(mesh), show_edges=True)
plotter.show()  

```

I tried to download GMSH in this way.

```auto
sudo apt install gmsh

```

But it didn’t work, and it gave a ‘No module named’ error.

---

<div class="post-metadata">

**Author:** ![dparsons](https://avatars.discourse-cdn.com/v4/letter/d/5e9695/32.png) [@dparsons](https://fenicsproject.discourse.group/u/dparsons)\
**Post date:** [December 29, 2024, 12:57pm UTC](https://fenicsproject.discourse.group/t/ubuntu-gmsh-import/16637/4 "2024-12-29T12:57:35Z")

</div>

> [@zeynep](#):
>
> I tried to download GMSH in this way.
> 
> ```auto
> sudo apt install gmsh
> 
> ```
> 
> But it didn’t work, and it gave a ‘No module named’ error.

You need to use the name of the python package used by ubuntu. The gmsh package only provides gmsh itself (the GUI client, and C libraries), not the python package.

You can search for packages at [https://packages.ubuntu.com/](https://packages.ubuntu.com/)  
Can you see there which ubuntu package provides the gmsh python module?

---

<div class="post-metadata">

**Author:** ![zeynep](https://avatars.discourse-cdn.com/v4/letter/z/73ab20/32.png) [@zeynep](https://fenicsproject.discourse.group/u/zeynep)\
**Post date:** [December 29, 2024, 3:07pm UTC](https://fenicsproject.discourse.group/t/ubuntu-gmsh-import/16637/6 "2024-12-29T15:07:46Z")

</div>

Hi thanks, I got how to install GMSH. However, I think I need to use Dolfinx to include the files exported by GMSH into the FEniCS environment. As far as I know, the Dolfinx module is included by default in FEniCSx, but I’m encountering an error when running the code I’m currently using. What solution would you suggest in this case?

```auto
import dolfinx
import numpy as np
from mpi4py import MPI
from petsc4py import PETSc

mesh = dolfinx.mesh.create_unit_square(MPI.COMM_WORLD, 10, 10)

V = dolfinx.fem.FunctionSpace(mesh, ("CG", 1))

u = dolfinx.fem.Function(V)

u.x.array[:] = 0.0

print("Function values (the function with zero values):")
print(u.x.array)

```

```auto
zeynep@LAPTOP-3KN21JEE:~$ cd /home/zeynep ; /usr/bin/env /bin/python3 /home/zeynep/.vscode-server/extensions/ms-python.debugpy-2024.14.0-linux-x64/bundled/libs/debugpy/adapter/../../debugpy/launcher 53555 -- /home/zeynep/FEM/fem1.py 
/home/zeynep/FEM/fem1.py:2: SyntaxWarning: invalid escape sequence '\l'
  '''
/home/zeynep/FEM/fem1.py:204: SyntaxWarning: invalid escape sequence '\l'
  '''
/home/zeynep/FEM/fem1.py:380: SyntaxWarning: invalid escape sequence '\l'
  '''
Traceback (most recent call last):
  File "/home/zeynep/FEM/fem1.py", line 575, in <module>
    V = dolfinx.fem.FunctionSpace(mesh, ("CG", 1))
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: FunctionSpace. __init__ () missing 1 required positional argument: 'cppV'

```

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [December 29, 2024, 3:46pm UTC](https://fenicsproject.discourse.group/t/ubuntu-gmsh-import/16637/7 "2024-12-29T15:46:06Z")

</div>

> [@zeynep](#):
>
> ` dolfinx.fem.FunctionSpace(mesh, ("CG", 1))`

This is outdated api. The new API is `dolfinx.fem.functionspace`, see:  
[https://jsdokken.com/dolfinx-tutorial/Changelog.html](https://jsdokken.com/dolfinx-tutorial/Changelog.html)  
and

> **[FEniCS release notes](https://fenicsproject.org/blog/v0.9.0/)**
>
> Open-source finite element library

---

<div class="post-metadata">

**Author:** ![dparsons](https://avatars.discourse-cdn.com/v4/letter/d/5e9695/32.png) [@dparsons](https://fenicsproject.discourse.group/u/dparsons)\
**Post date:** [December 29, 2024, 4:07pm UTC](https://fenicsproject.discourse.group/t/ubuntu-gmsh-import/16637/8 "2024-12-29T16:07:04Z")

</div>

> [@zeynep](#):
>
> Hi thanks, I got how to install GMSH

Report which package you needed to fix the installation, in case anyone else has the same problem and is searching for help in the future.

---

<div class="post-metadata">

**Author:** ![zeynep](https://avatars.discourse-cdn.com/v4/letter/z/73ab20/32.png) [@zeynep](https://fenicsproject.discourse.group/u/zeynep)\
**Post date:** [December 29, 2024, 8:53pm UTC](https://fenicsproject.discourse.group/t/ubuntu-gmsh-import/16637/9 "2024-12-29T20:53:43Z")

</div>

Yes sure. First, I installed the python3-pygmsh package. You can find the necessary information for this package at this link ([Ubuntu – Details of package python3-pygmsh in oracular](https://packages.ubuntu.com/oracular/python3-pygmsh)). To install it, run the following commands in the terminal:

```auto
sudo apt update
sudo apt install python3-pygmsh

```

After that, i download GMSH for Linux from the official GMSH website ([https://gmsh.info/](https://gmsh.info/)).  
Once the installation is complete, you can open the GMSH interface page by typing `gmsh` in the Ubuntu terminal.

---

<div class="post-metadata">

**Author:** ![dparsons](https://avatars.discourse-cdn.com/v4/letter/d/5e9695/32.png) [@dparsons](https://fenicsproject.discourse.group/u/dparsons)\
**Post date:** [December 29, 2024, 9:52pm UTC](https://fenicsproject.discourse.group/t/ubuntu-gmsh-import/16637/10 "2024-12-29T21:52:22Z")

</div>

> [@zeynep](#):
>
> I installed the python3-pygmsh package

hmm, that doesn’t sound right. python3-pygmsh provides the pygmsh module, not gmsh. It’s a different project (different API for handling gmsh files).

python3-gmsh is the one that provides the gmsh python module.

Note you already had the gmsh GUI installed from the gmsh package. No need to install it again from the GMSH website.

---

<div class="post-metadata">

**Author:** ![zeynep](https://avatars.discourse-cdn.com/v4/letter/z/73ab20/32.png) [@zeynep](https://fenicsproject.discourse.group/u/zeynep)\
**Post date:** [January 2, 2025, 1:28pm UTC](https://fenicsproject.discourse.group/t/ubuntu-gmsh-import/16637/11 "2025-01-02T13:28:03Z")

</div>

After installing this library, my ‘import gmsh’ error was resolved, and I also installed the python3-gmsh package additionally.

---

<div class="post-metadata">

**Author:** ![dparsons](https://avatars.discourse-cdn.com/v4/letter/d/5e9695/32.png) [@dparsons](https://fenicsproject.discourse.group/u/dparsons)\
**Post date:** [January 2, 2025, 3:49pm UTC](https://fenicsproject.discourse.group/t/ubuntu-gmsh-import/16637/13 "2025-01-02T15:49:35Z")

</div>

python3-pygmsh depends on python3-gmsh, so it installs the gmsh python module when you install it. It doesn’t do any harm to install pygmsh, it’s just not needed if you’re only using gmsh. In some cases the pygmsh API might be easier to use than the gmsh API for generating meshes.

---

<div class="post-metadata">

**Author:** ![zeynep](https://avatars.discourse-cdn.com/v4/letter/z/73ab20/32.png) [@zeynep](https://fenicsproject.discourse.group/u/zeynep)\
**Post date:** [January 2, 2025, 5:07pm UTC](https://fenicsproject.discourse.group/t/ubuntu-gmsh-import/16637/14 "2025-01-02T17:07:53Z")

</div>

Thank you for your responses, you’ve been really helpful.
